<?php

/**
 * 模型对应数据库表 "web_adsmanage".
 *
 * 以下是可用表web_adsmanage的栏位:
 * @property string $id
 * @property string $title
 * @property string $images
 * @property string $url
 * @property string $note
 * @property integer $sort
 * @property integer $state
 * @property string $addtime
 * @property string $webset
 * @property integer $sort_order
 */
class WebAdsmanage extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return WebAdsmanage the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_adsmanage';
    }

    /**
     * 判断激活
     * @var type 
     */
    public $state_array = array(
        1 => '启用',
        0 => '禁用',
    );

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('title, images,sort', 'required'),
            array('sort, state, sort_order', 'numerical', 'integerOnly' => true),
            array('title, images, url,note', 'length', 'max' => 255),
            array('addtime', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, images, url, note, sort, state, addtime, type,sort_order', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 激活状态标志
     * @param 状态id
     * @author nirsh
     */
    public function getCustomerState($aData) {
        $sUrl = Yii::app()->createUrl('webAdsmanage/setImportant', array(
            'id' => $aData->id,
            'state' => $aData->state,
                ));
        $adsManage_state = $aData->state;
        if ($adsManage_state) {
            $rString = '<a href="' . $sUrl . '" class="important" title="点击取消激活"></a>';
        } else {
            $rString = '<a href="' . $sUrl . '" class="set-important" title="点击激活"></a>';
        }
        return $rString;
    }

    /**
     * 选择判断激活
     * @param type $col   
     */
    public function resetState() {
        $sql = " UPDATE web_adsmanage SET state= 0 WHERE state=1";
        Yii::app()->db->createCommand($sql)->execute();
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'title' => '标题',
            'images' => '广告图片',
            'url' => '广告连接地址',
            'note' => '备注',
            'sort' => '广告位',
            'state' => '状态',
            'addtime' => '添加时间',
            'sort_order' => '排序',
        );
    }

    /**
     * 获取广告位分类
     * @return type 
     */
    public function getAll() {
        $data = array();
        foreach ($this->findAll() as $v) {
            $data[$v->id] = $v->ads_name;
        }
        return $data;
    }

    /*
      +-----------------------------------------------------
     * 说明 获取操作菜单
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */

    public function getColumnOptions($data) {
        $vUrl = Yii::app()->controller->rewriteCreateUrl('webAdsmanage/view', array('id' => $data->id));
        $uUrl = Yii::app()->controller->rewriteCreateUrl('webAdsmanage/update', array('id' => $data->id));
        $dUrl = Yii::app()->controller->rewriteCreateUrl('webAdsmanage/delete', array('WebAdsmanage[sort]' => $_GET["WebAdsmanage"]["sort"], 'id' => $data->id));
        $view.= '<a title="查看" href="' . $vUrl . '" class="gradient-btn">查看</a>';
        $editor = '<a title="编辑" href="' . $uUrl . '" class="gradient-btn">编辑</a>';
        $cancel = '<a rel="subsite" title="删除" href="javascript:void(0)" durl ="' . $dUrl . '" onclick="deleteByUrl(this)" class="gradient-btn">删除</a>';
        return $view . $editor . $cancel;
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('url', $this->url);
        $criteria->compare('sort', $this->sort);
        $criteria->compare('state', $this->state);
        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

}